package com.doge.walknovel.business.home.library

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.doge.wnpro.R
import com.doge.walknovel.core.base.adapters.AdapterCallback
import com.doge.walknovel.core.base.adapters.ITEM_CLICKED
import com.doge.wnpro.databinding.CellLibraryNovelBinding
import com.doge.walknovel.utils.*

class LibraryNovelViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    private val binding: CellLibraryNovelBinding = CellLibraryNovelBinding.inflate(
        LayoutInflater.from(parent.context), parent, false),
) : RecyclerView.ViewHolder(binding.root) {
    init {
        binding.item.setOnClickCallback {
            callback.invoke(ITEM_CLICKED, itemView.tag)
        }
        binding.item.setOnLongClickListener {
            callback.invoke(BOOK_EDIT, itemView.tag)
            true
        }
        binding.delete.setOnClickCallback {
            callback.invoke(BOOK_DEL, itemView.tag)
        }
    }

    fun bind(item: LibraryNovel) {
        itemView.tag = item.book
        binding.delete.visibility = if (item.edit) View.VISIBLE else View.INVISIBLE
        binding.cover.load(
            item.book?.coverUrl,
            R.drawable.shape_f7f7f7_8,
            transformation = RoundedCorners8,
            width = 100.dp,
            height = 143.dp,
        )
        binding.name.text = item.book?.title
        binding.percent.isVisible = (item.book?.percent ?: 0f > 0f)
        binding.percent.text = item.book?.percent.percentage
    }
}